package com.opesoft.app.system.action;

import java.util.List;

import com.opesoft.app.system.biz.DictionaryBO;
import com.opesoft.app.system.model.Dictionary;
import com.opesoft.fw.ajax.Ajax;
import com.opesoft.fw.ajax.AjaxWebManager;
import com.opesoft.fw.core.exception.MessageException;
import com.opesoft.pf.page.AjaxQueryResult;

public class DictionaryAction
{
	DictionaryBO bo=new DictionaryBO();

	@SuppressWarnings("unchecked")
	@Ajax(clientCall=true)
	public List allDictionaryConfig() throws Exception
	{
		return this.bo.allDictionaryConfig();
	}

	@Ajax(clientCall=true, forward=true)
	public String add(long dictionaryConfigId)
	{
		Dictionary dict=new Dictionary();
		dict.setDictionaryConfigId(dictionaryConfigId);
		AjaxWebManager.request().setAttribute("data",dict);
		AjaxWebManager.request().setAttribute("op","insert");
		return "dictionary.edit.jsp";
	}

	@Ajax(forward=true)
	public String insert() throws Exception
	{
		Dictionary dictionary=AjaxWebManager.populate(Dictionary.class);
		this.bo.insert(dictionary);
		return this.add(dictionary.getDictionaryConfigId());
	}

	@Ajax(clientCall=true)
	public AjaxQueryResult query(long dictionaryConfigId) throws MessageException
	{
		DictionaryQueryForm form=AjaxWebManager.populate(DictionaryQueryForm.class);
		form.setDictionaryConfigId(dictionaryConfigId);
		return form.query();
	}

	@Ajax(forward=true, clientCall=true)
	public String edit(String id) throws Exception
	{
		Dictionary dc=this.bo.find(id);
		if(dc!=null)
		{
			AjaxWebManager.request().setAttribute("op","update");
			AjaxWebManager.request().setAttribute("data",dc);
		}
		else return this.add(dc.getDictionaryConfigId());

		return "dictionary.edit.jsp";
	}

	@Ajax(clientCall=true)
	public void del(String[] ids) throws Exception
	{
		this.bo.del(ids);
	}

	@Ajax(forward=true, clientCall=true)
	public String update() throws Exception
	{
		Dictionary dictionary=this.bo.find(AjaxWebManager.request().getParameter("id"));
		AjaxWebManager.populate(dictionary);
		this.bo.update(dictionary);
		return this.add(dictionary.getDictionaryConfigId());
	}

	@Ajax(clientCall=true)
	public void moveUp(long configId,String[] ids) throws MessageException
	{
		(new DictionaryBO()).moveUp(configId,ids);
	}

	@Ajax(clientCall=true)
	public void moveDown(long configId,String[] ids) throws MessageException
	{
		(new DictionaryBO()).moveDown(configId,ids);
	}
}
